package com.teamcenter.rac.project.fragment.handlers;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Button;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.RowLayout;

public class LogicalRadioGroup extends Composite implements SelectionListener{

	private Button trueButton;
	private Button falseButton;

	private final List<IPropertyChangeListener> m_propertyChangeListeners = new ArrayList();
	
	private String property;
	private String oldValue = "";

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 * @param property 
	 */
	public LogicalRadioGroup(Composite parent, int style, String property) {
		super(parent, style);
		this.property = property;
		RowLayout rowLayout = new RowLayout(SWT.HORIZONTAL);
		rowLayout.spacing = 10;
		rowLayout.pack = false;
		rowLayout.center = true;
		rowLayout.fill = true;
		setLayout(rowLayout);

		trueButton = new Button(this, SWT.RADIO);
		trueButton.setText("是");
		falseButton = new Button(this, SWT.RADIO);
		falseButton.setText("否");

		trueButton.addSelectionListener(this);
		falseButton.addSelectionListener(this);
		
	}

	public boolean getValue() {
		return trueButton.getSelection();
	}

	public String getDisplableValue() {
		return getValue() ? "true" : "false";
	}

	public void setValue(boolean flag) {
		trueButton.setSelection(flag);
		falseButton.setSelection(!flag);
		this.oldValue = getDisplableValue();
	}

	public void addPropertyChangeListener(IPropertyChangeListener var1) {
		if (!this.m_propertyChangeListeners.contains(var1)) {
			this.m_propertyChangeListeners.add(var1);
		}
	}

	@Override
	protected void checkSubclass() {
	}

	@Override
	public void widgetDefaultSelected(SelectionEvent arg0) {
		
	}

	@Override
	public void widgetSelected(SelectionEvent arg0) {
		
		PropertyChangeEvent pce = new PropertyChangeEvent(arg0, property, oldValue, getDisplableValue());
		for (IPropertyChangeListener pcl : m_propertyChangeListeners) {
			pcl.propertyChange(pce);
		}
		oldValue = getDisplableValue();
	}
}
